package com.book.lizhi.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.book.lizhi.AppApplication;
import com.book.lizhi.R;
import com.book.lizhi.constants.Constants;
import com.book.lizhi.database.DataBaseMannager;
import com.book.lizhi.model.DetailBookModel;
import com.book.lizhi.read.ReadActivity;
import com.book.lizhi.ui.WebViewActivity;

public class DetailFragment extends Fragment {
	private View mView;
	private DetailBookModel mBookModel;

	private TextView mReadView;
	private TextView mAddBookshelfView;
	private TextView mBuyView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mView = View.inflate(getActivity(), R.layout.fragement_detail_content, null);
		mBookModel = JSON.parseObject(JSON.toJSONString(AppApplication.instance.currentBookModel), DetailBookModel.class);
		
		if (AppApplication.instance.bookshelfIDs.contains(mBookModel.id)) {
			mBookModel.isOnBookshelf = true;
		}

		initUI();
	}

	private void initUI() {
		TextView nameView = (TextView) mView.findViewById(R.id.detail_book_name_view);
		nameView.setText(mBookModel.name);
		TextView desView = (TextView) mView.findViewById(R.id.library_book_des_view);
		desView.setText(mBookModel.brief);
		ImageView coverView = (ImageView) mView.findViewById(R.id.detail_book_cover_view);
		ImageListener imageListener = ImageLoader.getImageListener(coverView, R.drawable.cover_default, R.drawable.cover_default);
		AppApplication.instance.imageLoader.get(mBookModel.img, imageListener);

		mReadView = (TextView) mView.findViewById(R.id.detail_read_view);
		mAddBookshelfView = (TextView) mView.findViewById(R.id.detail_add_bookshelf_view);

		if (mBookModel.isOnBookshelf) {
			mAddBookshelfView.setEnabled(false);
			mAddBookshelfView.setText("已经加入书架");
		} else {
			mAddBookshelfView.setEnabled(true);
			mAddBookshelfView.setText("加入书架");
		}

		mBuyView = (TextView) mView.findViewById(R.id.detail_buy_view);

		addOnListener();
	}

	private void addToBookshelf() {
		mBookModel.isOnBookshelf = true;
		mBookModel.rcIndex = String.valueOf(0);
		mBookModel.lcIndex = String.valueOf(0);
		mBookModel.position = String.valueOf(0);
		DataBaseMannager.getInstance().insertValue(getActivity(), mBookModel);

		mAddBookshelfView.setText("已经加入书架");
		mAddBookshelfView.setEnabled(false);
		
		AppApplication.instance.currentBookModel.isOnBookshelf=true;
	}

	private void addOnListener() {
		mReadView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(getActivity(), ReadActivity.class));
				if (!mBookModel.isOnBookshelf) {
					addToBookshelf();
				}
			}
		});

		mAddBookshelfView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				addToBookshelf();
			}

		});
		mBuyView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String url = mBookModel.business;
				if (TextUtils.isEmpty(url)) {
					url = "http://s.click.taobao.com/t?e=m%3D2%26s%3DcDVusM%2BUEBkcQipKwQzePDAVflQIoZepK7Vc7tFgwiFRAdhuF14FMaAI%2FtcMbXUZ5x%2BIUlGKNpW8TUbkMajyHFRdAN7hwPSDmSDfbH0f3AiuRefWAhyyI3H5aVC8wvjy";
				}
				Intent intent = new Intent(getActivity(), WebViewActivity.class);
				intent.putExtra(Constants.SOURCE_URL, url);
				intent.putExtra(Constants.TITLE, "手机淘宝网");
				// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
				// intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
				getActivity().startActivity(intent);
			}
		});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		if (mView.getParent() != null) {
			((ViewGroup) mView.getParent()).removeView(mView);
		}
		return mView;
	}
}
